package com.yaozheng.vocationaltraining.activity;

import android.content.Intent;
import android.view.View;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.iview.IMasterView;
import com.yaozheng.vocationaltraining.service.MasterService;
import com.yaozheng.vocationaltraining.service.impl.MasterServiceImpl;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.ExamProgressStatusView;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 大师之路
 */
@EActivity(R.layout.activity_master)
public class MasterActivity extends BaseActivity implements IMasterView {
    @Bean(MasterServiceImpl.class)
    MasterService masterService;

    JSONArray mastersArray;
    @ViewById
    ExamProgressStatusView maintenanceMasterProgressStatusView1, maintenanceMasterProgressStatusView2,
            serviceMasterProgressStatusView3, salesMasterProgressStatusView4, goodsMasterProgressStatusView5,
            brandMasterProgressStatusView6;

    ExamProgressStatusView[] maintenanceMasterProgressStatusViews;

    @AfterViews
    public void initView() {
        setHeadTitle("大师之路");
        masterService.init(this);
        loadData();
        maintenanceMasterProgressStatusViews = new ExamProgressStatusView[]{maintenanceMasterProgressStatusView1,
                maintenanceMasterProgressStatusView2, serviceMasterProgressStatusView3, salesMasterProgressStatusView4,
                goodsMasterProgressStatusView5, brandMasterProgressStatusView6};
    }

    public void loadData() {
        masterService.masterProgress();
    }

    /**
     * 进入闯关之旅
     */
    public void enterCheckpointsTripActivity(View view) {
        Intent intent = new Intent();
        intent.setClassName(this,
                Constants.MASTERS_TOUR_ACTIVITY_CLASS_NAME);
        try {
            intent.putExtra("i", Integer.parseInt(String.valueOf(view.getTag())));
        } catch (Exception e) {
            intent.putExtra("i", 1);
        }
        startActivity(intent);
    }

    @UiThread
    @Override
    public void masterProgressSuccess(JSONObject jsonObject) {
        cancelLoadDataProgressDialog();
        this.mastersArray = TypeUtils.getJsonArray(jsonObject, "data", "masters");
        updateData();
    }

    /**
     * 刷新数据
     */
    public void updateData() {
//        masters
        if (mastersArray == null) {
            for (int i = 0; i < maintenanceMasterProgressStatusViews.length; i++) {
                ExamProgressStatusView maintenanceMasterProgressStatusView = maintenanceMasterProgressStatusViews[i];
                maintenanceMasterProgressStatusView.setStatus(-1);
            }
        } else {
            for (int i = 0; i < TypeUtils.getJsonArraySize(mastersArray); i++) {
                JSONObject masterJsonObject = TypeUtils.getJsonObject(mastersArray, i);
                int id = TypeUtils.getJsonInteger(masterJsonObject, "id");
                int step = TypeUtils.getJsonInteger(masterJsonObject, "step");
                if (id < maintenanceMasterProgressStatusViews.length) {
                    ExamProgressStatusView maintenanceMasterProgressStatusView = maintenanceMasterProgressStatusViews[id];
                    if (step == 0) {
                        maintenanceMasterProgressStatusView.setStatus(-1);
                    } else if (step >= 1 && step < 9) {
                        maintenanceMasterProgressStatusView.setStatus(-2);
                    } else if (step >= 9) {
                        maintenanceMasterProgressStatusView.setStatus(-3);
                    }
                }
            }
        }

    }

    @UiThread
    @Override
    public void masterProgressError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }
}
